package com.mzy.linear_struct.subject;

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: codermzy
 * @Date: 2024/03/29/17:32
 * @Description:
 */
public class 约瑟夫特环 {

    public static int lastRemaining(int n, int m) {
        Queue<Integer> queue = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            queue.offer(i);
        }

        int count = 0;
        while (queue.size() > 1) {
            count++;
            if (count % m == 0){
                queue.poll();
            }else {
                queue.offer(queue.poll());
            }
        }
        return queue.poll();
    }

    public static void main(String[] args) {
        System.out.println(lastRemaining(5, 3));
        System.out.println(lastRemaining(10, 17));

    }
}
